﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;

public partial class ybqy_1_3 : System.Web.UI.Page
{
    public void wujiluxinzheng()
    {
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd1 = new SqlCommand("select * from a01103 where  ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"], sqlcon);


        SqlDataReader reader1 = sqlcmd1.ExecuteReader(CommandBehavior.CloseConnection);

        if (!reader1.Read())
        {
            //sqlcon.Open();
            string sqlstr = @"insert into a01103 (PRODUCT_NAME,PRODUCT_UNIT,PLAN_COUNT,PRODUCT_COUNT,PRODUCT_WATER,PRODUCT_SOURCE,PRODUCT_COAL,qiyeid,ShenBaoNianDu,PRODUCT_ID,jy) VALUES ("
                + "'" + PRODUCT_NAME.Text.Trim()
                + "','" + PRODUCT_UNIT.Text.Trim()
                + "','" + PLAN_COUNT.Text.Trim()
                + "','" + PRODUCT_COUNT.Text.Trim()
                + "','" + PRODUCT_WATER.Text.Trim()
                + "','" + PRODUCT_SOURCE.Text.Trim()
                + "','" + PRODUCT_COAL.Text.Trim() + "'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','weijiaoyan')";

            SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlcon);
            reader1.Close();
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();

            sqlstr = "select max(id) as zdid from a01103 where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
            sqlcmd.CommandText = sqlstr;

            SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

            if (reader.Read())
            {
                string zdid = reader["zdid"].ToString();

                sqlstr = @"insert into a01103_f(DATUM_NAME,DATUM_UNIT,DATUM_USE,zhubiaoid,DATUM_ID,qiyeid,ShenBaoNianDu,PRODUCT_ID,data_month,jy) VALUES ("
                + "'" + DATUM_NAME1.Text.Trim()
                + "','" + DATUM_UNIT1.Text.Trim()
                + "','" + DATUM_USE1.Text.Trim()
                + "'," + zdid + ",'001'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','" + Session["ShenBaoNianDu"] + "','weijiaoyan')";

                sqlcmd.CommandText = sqlstr;
                reader.Close();
                sqlcon.Open();
                sqlcmd.ExecuteNonQuery();


                sqlstr = @"insert into a01103_f(DATUM_NAME,DATUM_UNIT,DATUM_USE,zhubiaoid,DATUM_ID,qiyeid,ShenBaoNianDu,PRODUCT_ID,data_month,jy) VALUES ("
                + "'" + DATUM_NAME2.Text.Trim()
                + "','" + DATUM_UNIT2.Text.Trim()
                + "','" + DATUM_USE2.Text.Trim()
                + "'," + zdid + ",'002'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','" + Session["ShenBaoNianDu"] + "','weijiaoyan')";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();


                sqlstr = @"insert into a01103_f(DATUM_NAME,DATUM_UNIT,DATUM_USE,zhubiaoid,DATUM_ID,qiyeid,ShenBaoNianDu,PRODUCT_ID,data_month,jy) VALUES ("
                + "'" + DATUM_NAME3.Text.Trim()
                + "','" + DATUM_UNIT3.Text.Trim()
                + "','" + DATUM_USE3.Text.Trim()
                + "'," + zdid + ",'003'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','" + Session["ShenBaoNianDu"] + "','weijiaoyan')";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();

                HiddenField_xiugai.Value = "edit";
                GridView1.DataBind();
                HiddenField_id.Value = zdid;
            }
        }
        else
        {
            if (reader1["PRODUCT_NAME"].ToString() == "" || reader1["PRODUCT_NAME"].ToString() == null)
            {
                HiddenField_id.Value = reader1["id"].ToString();
                HiddenField_xiugai.Value = "edit";

            }

        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        wujiluxinzheng();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (((GridView)e.Row.FindControl("GridView4")) != null)
        {
            // 无论什么乱七八糟的代码，主要是在这里绑定GridView里的那个GridView
            string tProjectID = (e.Row.FindControl("lbhidden") as Label).Text;
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
            sqlcon.Open();

            SqlCommand sqlcmd = new SqlCommand("select * from a01103_f where zhubiaoid=" + tProjectID + " and ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"], sqlcon);
            string sqlstr = "select * from a01103_f where zhubiaoid=" + tProjectID + " and ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];

            GridView tempGV = (e.Row.FindControl("GridView4") as GridView);
            tempGV.DataSource = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
            tempGV.DataBind();
        }

        //删除时弹出确认按钮
        if (e.Row.RowType == DataControlRowType.DataRow)//如果是绑定数据行
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[10].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        HiddenField_id.Value = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        HiddenField_xiugai.Value = "edit";
        //LinkButton2.Visible = true;

        GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
        if (row.Cells[1].Text != "&nbsp;")
        {
            PRODUCT_NAME.Text = row.Cells[1].Text;
        }
        if (row.Cells[2].Text != "&nbsp;")
        {
            PRODUCT_UNIT.Text = row.Cells[2].Text;
        }
        PLAN_COUNT.Text = row.Cells[3].Text;
        PRODUCT_COUNT.Text = row.Cells[4].Text;
        PRODUCT_WATER.Text = row.Cells[6].Text;
        PRODUCT_SOURCE.Text = row.Cells[7].Text;
        PRODUCT_COAL.Text = row.Cells[8].Text;

        DATUM_NAME1.Text = "";
        DATUM_UNIT1.Text = "";
        DATUM_USE1.Text = "";
        DATUM_NAME2.Text = "";
        DATUM_UNIT2.Text = "";
        DATUM_USE2.Text = "";
        DATUM_NAME3.Text = "";
        DATUM_UNIT3.Text = "";
        DATUM_USE3.Text = "";


        //string tProjectID = (e.Row.FindControl("lbhidden") as Label).Text;
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd = new SqlCommand("select * from a01103_f where zhubiaoid=" + HiddenField_id.Value + " and ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"] + " order by DATUM_ID", sqlcon);


        SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
        int i = 0;
        while (reader.Read())
        {
            if (i == 0)
            {
                DATUM_NAME1.Text = reader["DATUM_NAME"].ToString();
                DATUM_UNIT1.Text = reader["DATUM_UNIT"].ToString();
                DATUM_USE1.Text = reader["DATUM_USE"].ToString();
            }
            if (i == 1)
            {
                DATUM_NAME2.Text = reader["DATUM_NAME"].ToString();
                DATUM_UNIT2.Text = reader["DATUM_UNIT"].ToString();
                DATUM_USE2.Text = reader["DATUM_USE"].ToString();
            }
            if (i == 2)
            {
                DATUM_NAME3.Text = reader["DATUM_NAME"].ToString();
                DATUM_UNIT3.Text = reader["DATUM_UNIT"].ToString();
                DATUM_USE3.Text = reader["DATUM_USE"].ToString();
            }
            i = i + 1;
        }


    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();

        SqlCommand sqlcmd = new SqlCommand("delete from A01103 where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'", sqlcon);

        sqlcmd.ExecuteNonQuery();

        string sqlstr = "delete from A01103_f where zhubiaoid='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcmd.CommandText = sqlstr;
        sqlcmd.ExecuteNonQuery();

        sqlcon.Close();


        GridView1.DataBind();

        wujiluxinzheng();

    }

    public string DisplayStr()
    {

        YingYongHanShu yyhs = new YingYongHanShu();

        return yyhs.DisplayStr2("101103");
    }


    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Label3.Text = "";
        if (!Page.IsValid) { return; }

        SqlConnection sqlcon = new SqlConnection();
        sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        sqlcon.Open();
        if (HiddenField_xiugai.Value == "edit")//如果是更新状态，更新状态只在点选择是置为EDIT
        {

            string sqlstr = @"update a01103 set " +
                "PRODUCT_NAME='" + PRODUCT_NAME.Text + "'," +
                "PRODUCT_UNIT='" + PRODUCT_UNIT.Text + "'," +
                "PLAN_COUNT='" + PLAN_COUNT.Text + "'," +
                "PRODUCT_COUNT='" + PRODUCT_COUNT.Text + "'," +
                "PRODUCT_WATER='" + PRODUCT_WATER.Text + "'," +
                "PRODUCT_SOURCE='" + PRODUCT_SOURCE.Text + "'," +
                "PRODUCT_COAL='" + PRODUCT_COAL.Text + "',jy='ruanjian' " +
                "where id=" + HiddenField_id.Value;

            SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlcon);
            sqlcmd.ExecuteNonQuery();

            sqlstr = @"update a01103_f set " +
               "DATUM_NAME='" + DATUM_NAME1.Text + "'," +
               "DATUM_UNIT='" + DATUM_UNIT1.Text + "'," +
               "DATUM_USE='" + DATUM_USE1.Text + "',jy='ruanjian' " +
               "where DATUM_ID='001' and zhubiaoid=" + HiddenField_id.Value;

            sqlcmd.CommandText = sqlstr;
            sqlcmd.ExecuteNonQuery();
            sqlstr = @"update a01103_f set " +
               "DATUM_NAME='" + DATUM_NAME2.Text + "'," +
               "DATUM_UNIT='" + DATUM_UNIT2.Text + "'," +
               "DATUM_USE='" + DATUM_USE2.Text + "',jy='ruanjian' " +
               "where DATUM_ID='002' and zhubiaoid=" + HiddenField_id.Value;

            sqlcmd.CommandText = sqlstr;
            sqlcmd.ExecuteNonQuery();
            sqlstr = @"update a01103_f set " +
               "DATUM_NAME='" + DATUM_NAME3.Text + "'," +
               "DATUM_UNIT='" + DATUM_UNIT3.Text + "'," +
               "DATUM_USE='" + DATUM_USE3.Text + "',jy='ruanjian' " +
               "where DATUM_ID='003' and zhubiaoid=" + HiddenField_id.Value;

            sqlcmd.CommandText = sqlstr;
            sqlcmd.ExecuteNonQuery();

        }
        else
        {
            string sqlstr = @"insert into a01103 (PRODUCT_NAME,PRODUCT_UNIT,PLAN_COUNT,PRODUCT_COUNT,PRODUCT_WATER,PRODUCT_SOURCE,PRODUCT_COAL,qiyeid,ShenBaoNianDu,PRODUCT_ID,jy) VALUES ("
            + "'" + PRODUCT_NAME.Text.Trim()
            + "','" + PRODUCT_UNIT.Text.Trim()
            + "','" + PLAN_COUNT.Text.Trim()
            + "','" + PRODUCT_COUNT.Text.Trim()
            + "','" + PRODUCT_WATER.Text.Trim()
            + "','" + PRODUCT_SOURCE.Text.Trim()
            + "','" + PRODUCT_COAL.Text.Trim() + "'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','ruanjian')";

            SqlCommand sqlcmd = new SqlCommand(sqlstr, sqlcon);
            sqlcmd.ExecuteNonQuery();

            sqlstr = "select max(id) as zdid from a01103 where ShenBaoNianDu='" + Session["ShenBaoNianDu"] + "' and qiyeid=" + Session["qiyeid"];
            sqlcmd.CommandText = sqlstr;

            SqlDataReader reader = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

            if (reader.Read())
            {
                string zdid = reader["zdid"].ToString();

                sqlstr = @"insert into a01103_f(DATUM_NAME,DATUM_UNIT,DATUM_USE,zhubiaoid,DATUM_ID,qiyeid,ShenBaoNianDu,PRODUCT_ID,data_month,jy) VALUES ("
                + "'" + DATUM_NAME1.Text.Trim()
                + "','" + DATUM_UNIT1.Text.Trim()
                + "','" + DATUM_USE1.Text.Trim()
                + "'," + zdid + ",'001'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','" + Session["ShenBaoNianDu"] + "','ruanjian')";

                sqlcmd.CommandText = sqlstr;
                reader.Close();
                sqlcon.Open();
                sqlcmd.ExecuteNonQuery();


                sqlstr = @"insert into a01103_f(DATUM_NAME,DATUM_UNIT,DATUM_USE,zhubiaoid,DATUM_ID,qiyeid,ShenBaoNianDu,PRODUCT_ID,data_month,jy) VALUES ("
                + "'" + DATUM_NAME2.Text.Trim()
                + "','" + DATUM_UNIT2.Text.Trim()
                + "','" + DATUM_USE2.Text.Trim()
                + "'," + zdid + ",'002'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','" + Session["ShenBaoNianDu"] + "','ruanjian')";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();


                sqlstr = @"insert into a01103_f(DATUM_NAME,DATUM_UNIT,DATUM_USE,zhubiaoid,DATUM_ID,qiyeid,ShenBaoNianDu,PRODUCT_ID,data_month,jy) VALUES ("
                + "'" + DATUM_NAME3.Text.Trim()
                + "','" + DATUM_UNIT3.Text.Trim()
                + "','" + DATUM_USE3.Text.Trim()
                + "'," + zdid + ",'003'," + Session["qiyeid"] + ",'" + Session["ShenBaoNianDu"] + "','1','" + Session["ShenBaoNianDu"] + "','ruanjian')";

                sqlcmd.CommandText = sqlstr;
                sqlcmd.ExecuteNonQuery();


            }




        }
        sqlcon.Close();
        GridView1.DataBind();
        HiddenField_xiugai.Value = "";

        DATUM_NAME1.Text = "";
        DATUM_UNIT1.Text = "";
        DATUM_USE1.Text = "";
        DATUM_NAME2.Text = "";
        DATUM_UNIT2.Text = "";
        DATUM_USE2.Text = "";
        DATUM_NAME3.Text = "";
        DATUM_UNIT3.Text = "";
        DATUM_USE3.Text = "";


        PRODUCT_NAME.Text = "";
        PRODUCT_UNIT.Text = "";
        PLAN_COUNT.Text = "";
        PRODUCT_COUNT.Text = "";
        PRODUCT_WATER.Text = "";
        PRODUCT_SOURCE.Text = "";
        PRODUCT_COAL.Text = "";
        Label3.Text = "提交完成";
    }
}